package com.paomi.client.fragment.order.Adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.makeramen.roundedimageview.RoundedImageView;
import com.paomi.client.GlideApp;
import com.paomi.client.R;
import com.paomi.client.activity.detail.OrderDetailActivity;
import com.paomi.client.fragment.order.model.OrderListBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.ViewHolder> {
    private Activity mcontext;
    private List<OrderListBean.OrderGoods> list = new ArrayList<>();
    private OrderListBean.OrderData item;

    public OrderListAdapter(Activity comtext, OrderListBean.OrderData item) {
        mcontext = comtext;
        this.item = item;
    }

    public void setData(List<OrderListBean.OrderGoods> jsonList) {
        list = jsonList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.item_list_order, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        OrderListBean.OrderGoods orderGoods=list.get(position);
        GlideApp.with(mcontext).load(item.getGoodsImg()).placeholder(R.mipmap.img_default_goods).into(holder.performIv);
        holder.shopName.setText(item.getGoodsName());
        holder.shopMoney.setText("¥"+orderGoods.getAmount());
        holder.showNum.setText("X  "+orderGoods.getNum());
        holder.show_site.setText(orderGoods.getGoodsTicketName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(mcontext, OrderDetailActivity.class);
                i.putExtra("orderId",""+item.getOrderId());
                mcontext.startActivityForResult(i,1003);
            }
        });
        //显示有没有优惠券
        if(item.getCouponsAmount()>0){
            holder.coupon_get.setVisibility(View.VISIBLE);
        }else{
            holder.coupon_get.setVisibility(View.GONE);
        }
        //显示积分抵用
        if(item.getIntegralAmount()>0){
            holder.inter_get.setVisibility(View.VISIBLE);
        }else{
            holder.inter_get.setVisibility(View.GONE);
        }
        //显示是快递选择
        if(item.getFreightAmount()>0){
            holder.send_get.setVisibility(View.VISIBLE);
        }else{
            holder.send_get.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.perform_iv)
        RoundedImageView performIv;
        @BindView(R.id.shop_name)
        TextView shopName;
        @BindView(R.id.shop_money)
        TextView shopMoney;
        @BindView(R.id.show_num)
        TextView showNum;
        @BindView(R.id.show_site)
        TextView show_site;
        @BindView(R.id.coupon_get)
        TextView coupon_get;//券
        @BindView(R.id.inter_get)
        TextView inter_get;//积分
        @BindView(R.id.send_get)
        TextView send_get;//快递
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
